package com.example.music_liwei.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.music_liwei.R;
import com.example.music_liwei.fragment.BlankFragmentDesc.LyricsLine;

import java.util.List;

public class LyricsAdapter extends RecyclerView.Adapter<LyricsAdapter.LyricsViewHolder> {
    private List<LyricsLine> lyricsLines;
    private int currentPlayingIndex = -1;

    public LyricsAdapter(List<LyricsLine> lyricsLines) {
        this.lyricsLines = lyricsLines;
    }

    @NonNull
    @Override
    public LyricsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_lyrics_line, parent, false);
        return new LyricsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LyricsViewHolder holder, int position) {
        LyricsLine lyricsLine = lyricsLines.get(position);
        holder.tvLyricsLine.setText(lyricsLine.lyrics);
        if (position == currentPlayingIndex) {
            holder.tvLyricsLine.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.black1));
        } else {
            holder.tvLyricsLine.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.black));
        }
    }

    @Override
    public int getItemCount() {
        return lyricsLines.size();
    }

    public void setCurrentPlayingIndex(int index) {
        currentPlayingIndex = index;
        notifyDataSetChanged();
    }

    static class LyricsViewHolder extends RecyclerView.ViewHolder {
        TextView tvLyricsLine;

        public LyricsViewHolder(@NonNull View itemView) {
            super(itemView);
            tvLyricsLine = itemView.findViewById(R.id.tv_lyrics_line);
        }
    }
}
